package com.lvyq.qrobot;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author lvyq
 * @version 1.0
 * Create by 2024-06-17 12:23
 */
public class Demo2 {

    public static final String tnk="pEtosMx6pdhUkQBKR_DVW82ZEO3H6HJgiWloaHMXu3Y";
    public static final String i="22167973";
    public static final String tnk_2="Ybk_aD00p9a9LwqbLBanGD6Hetb3na5PiWloaHMXu3Y";
    public static final String i_2="22177190";
    public static void main(String[] args) throws InterruptedException {

        go(tnk,i);
/*       while (true){
           sendGet("http://game.qqhome.cn/mergecyhc5/instance/heal_w2.jsp?i=22177190&tkn="+tnk+"&c=1&v=-1");
            Thread.sleep(2000l);
       }*/

    }


    public static  void  go(String tnk, String i){
        List<Integer> ids=  new ArrayList<>();
        String cj = "http://game.qqhome.cn/mergecyhc5/field/main_w2.jsp?i="+i+"&tkn="+tnk+"&c=mv&v=404";
        String cj2 = ChuanYueUtil.sendGet(cj);
        Document parse1 = Jsoup.parse(cj2);
        Elements links = parse1.select("a[href]");
        List<String> monsters = new ArrayList<>();
        monsters.add("虎力仙");
        monsters.add("金鱼怪");
        monsters.add("鹿力仙");
        monsters.add("羊力仙");

        List<Integer> killIds= new ArrayList<>();
        for (Element link : links) {
            String text = link.text();

            if (monsters.contains(text)){
                System.out.println(text);
                String href = link.attr("href");
                String s = href.split("tg=")[1];
                Integer integer = Integer.valueOf(s);
                ids.add(integer);

                if (text.equals("青狮王1") || text.equals("白象王")){
                    killIds.add(integer);
                }
            }
        }

        String url = "http://game.qqhome.cn/mergecyhc5/field/fight_w2.jsp?i="+i+"&tkn="+tnk+"&c=a&k=m&tg=AAAA";

        System.out.println("怪物数量 = " + ids.size());
        AtomicInteger index = new AtomicInteger();



        ids.forEach(id->{
            String finalUrl = url.replace("AAAA",String.valueOf(id));;

            for (int j = 0; j < 5; j++) {
               // sendGet("http://game.qqhome.cn/mergecyhc5/instance/heal_w2.jsp?i=22177190&tkn=xVKtE7ARLIoduIshHoJ6bj6Hetb3na5PiWloaHMXu3Y&c=1&v=-1");
                String s =ChuanYueUtil.sendGet(finalUrl);
                Document doc = Jsoup.parse(s);
                String s1 = doc.body().getElementsByClass("write-mood").toString();
                Document parse = Jsoup.parse(s1);

                String text = parse.text();
                if (text.startsWith("目标怪物已被杀死,请刷新再试~")){
                    System.out.println("目标怪物已被杀死,请刷新再试~");
                    break;

                }
                String[] split = text.split("气血:");
                String[] guai = split[1].split("/");
                String guai_xue = guai[0];
                String[] people = split[2].split("/");
                String person = people[0];
                String people_xue_sy = person.substring(1, person.length());

                String guai_xue_sy = guai_xue.substring(1, guai_xue.length());
                System.out.println("当前怪物剩余血量" + guai_xue_sy);

                System.out.println("1号当前人物剩余血量" + people_xue_sy);
                try {
                    Integer gwBlood= Integer.valueOf(guai_xue_sy);
                  //if (killIds.contains(id)){
                    if (gwBlood<4700 ){
                            System.out.println("开始补刀》》》》》》》》");
                            go(tnk_2,i_2, String.valueOf(id));
                            System.out.println("补刀完成》》》》》》");
                            break;
                        }
                  //  }

                }catch (Exception e){
                    System.out.println("目标怪物已被杀死,请刷新再试~");
                    break;
                }

                try{

                    Integer   integer = Integer.valueOf(people_xue_sy);
                   /* if (integer<2000){
                        String  addBlood = "http://game.qqhome.cn/mergecyhc5/field/fight_w2.jsp?i="+i+"&tkn="+tnk+"&c=us&k=m&tg=10786&v=5039";
                        String s2 = sendGet(addBlood);
                        System.out.println("1号加血："+Jsoup.parse(s2).text());
                    }*/
                    }catch (Exception e){
                    String mp ="http://game.qqhome.cn/mergecyhc5/field/relive_w2.jsp?i="+i+"&tkn="+tnk+"&c=r";
                    System.out.println("喝孟婆汤,重新开始");
                    ChuanYueUtil.sendGet(mp);
                    //刷
                    ChuanYueUtil.sendGet("http://game.qqhome.cn/mergecyhc5/field/main_w2.jsp?i=" + i + "&tkn=" + tnk + "&c=mv&v=401");
                   // sendGet("http://game.qqhome.cn/mergecyhc5/field/main_w2.jsp?i=" + i + "&tkn=" + tnk + "&c=mv&v=304");
                    go(tnk,i);
                    }
                try {
                    Thread.sleep(2000L);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            index.getAndIncrement();
            if (index.get() == ids.size()){
                System.out.println("打了："+index.get()+"个怪，重新加载怪物");
            }else {
                System.out.println("打了："+index.get()+"个怪,总共"+ids.size()+"个怪物");
            }
        });
     go(tnk,i);
    }


    //补刀
    public static  void  go(String tnk, String i,String id){
        String cj = "http://game.qqhome.cn/mergecyhc5/field/main_w2.jsp?i="+i+"&tkn="+tnk+"&c=mv&v=404";
        ChuanYueUtil.sendGet(cj);
        String url = "http://game.qqhome.cn/mergecyhc5/field/fight_w2.jsp?i="+i+"&tkn="+tnk+"&c=a&k=m&tg=AAAA";
        String finalUrl = url.replace("AAAA",String.valueOf(id));;
            for (int j = 0; j < 5; j++) {
                String s = ChuanYueUtil.sendGet(finalUrl);
                Document doc = Jsoup.parse(s);
                String s1 = doc.body().getElementsByClass("write-mood").toString();
                Document parse = Jsoup.parse(s1);
                String text = parse.text();
                if (text.startsWith("目标怪物已被杀死,请刷新再试~")){
                    System.out.println("补刀失败》》》》》目标怪物已被杀死,请刷新再试~");
                    break;
                }
                String[] split = text.split("气血:");
                String[] guai = split[1].split("/");
                String guai_xue = guai[0];
                String[] people = split[2].split("/");
                String person = people[0];
                String people_xue_sy = person.substring(1, person.length());
                String guai_xue_sy = guai_xue.substring(1, guai_xue.length());
                System.out.println("补刀》》》》》当前怪物剩余血量" + guai_xue_sy);
                System.out.println("补刀》》》》》人物剩余血量" + people_xue_sy);
                try{
                    Integer   integer = Integer.valueOf(people_xue_sy);
                    if (integer<3500){
                        String  addBlood = "http://game.qqhome.cn/mergecyhc5/field/fight_w2.jsp?i="+i+"&tkn="+tnk+"&c=us&k=m&tg=17111&v=5039";
                        String s2 = ChuanYueUtil.sendGet(addBlood);
                        System.out.println("补刀》》》》》加血:"+Jsoup.parse(s2).text());
                    }
                }catch (Exception e){
                    String mp ="http://game.qqhome.cn/mergecyhc5/field/relive_w2.jsp?i="+i+"&tkn="+tnk+"&c=r";
                    System.out.println("补刀》》》》》喝孟婆汤,重新开始");
                    ChuanYueUtil.sendGet(mp);
                    //刷
                    ChuanYueUtil.sendGet("http://game.qqhome.cn/mergecyhc5/field/main_w2.jsp?i=" + i + "&tkn=" + tnk + "&c=mv&v=401");
                   // sendGet("http://game.qqhome.cn/mergecyhc5/field/main_w2.jsp?i=" + i + "&tkn=" + tnk + "&c=mv&v=304");
                    go(tnk,i,id);
                }
                try {
                    Thread.sleep(2000L);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
    }



    private static void JsonType() throws IOException {
        String token = "ZXlKMGVYQWlPaUpLVjFRaUxDSmhiR2NpT2lKSV****";
        URL url = new URL("https://api.devbit.cn/ai/api/ai/chat");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Authorization", "Bearer " + token);
        conn.setRequestProperty("Accept", "*/*");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setDoOutput(true);
        OutputStream os = conn.getOutputStream();
        String requestBody = "{\"prompt\":\"请扮演一个解决方案专家，无论我提出什么问题，请直接给出方案，不要有其他多余的文字\",\"question\":\"怎么把 5 个苹果合理地分给 6 个小朋友\",\"stream\":false}";
        os.write(requestBody.getBytes());
        os.flush();
        os.close();
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        in.close();
    }
}
